﻿<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>添加收货人</title>
	<meta name="author" content="XUE">
	<meta name="description" content="普济一城">
	<meta name="viewport" content="initial-scale=1,maximum-scale=1">
	<meta name="apple-mobile-web-app-capable" content="yes">
	<meta name="apple-mobile-web-app-status-bar-style" content="transparent">
	<meta name="format-detection" content="telephone=no,email=no">
	<link rel="stylesheet" type="text/css" href="../../css/base.css">
	<link rel="stylesheet" type="text/css" href="../../css/product-order/newConsignee.css">
	<link rel="stylesheet" type="text/css" href="../../css/layer.css">
</head>
<body>
	<p class="checks"></p>
	<!-- head -->
	<div class="head">
		<p>新增收货人</p>
		<div class="head-img">
			<img src="../../img/d_share.png">
		</div>
	</div>
	<!-- main -->
	<div class="main">
		<div class="main-a">
			<span>姓名</span><input type="text" placeholder="输入收货人姓名" name="" class="username">
		</div>
		<div class="main-a">
			<span>手机号码</span><input type="text" placeholder="输入手机号码" name="" class="userphone">
		</div>
		<div class="main-a">
			<span>所在区域</span><i class="shouhuodizhi_100">选择所在省份、城市、县区</i>
			<div class="kzpic">
				<img src="../../img/d_cart3.png">
			</div>
		</div>
		<div class="main-a">
			<span>信息地址</span><input type="text" placeholder="街道、楼牌号等详细地址" name="" class="address">
		</div>
		<div class="main-a">
			<span>邮政编码</span><input type="text" placeholder="邮政编码" name="" class="yznum">
		</div>
	</div>

	<div class="shengfenqu">
	<!-- head -->
        <div class="head">
            <div class="head-a">
                <p>选择省份</p>
            </div>
        </div>
        <!-- main -->
        <div class="mainM">
            <ul class="shengfendi">
            	<!-- <li class="lidi">
            		<span id="1" class="dizhil">北京</span><span>&gt;</span>
            	</li> -->
           </ul>
        </div>
	</div>
	<!-- 默认 -->
	<div class="default">
		<div class="default-a">
			<p class="default-left">设为默认收货地址</p>
			<div class="check"><input type="checkbox" class="checkbox" name="" ></div>
			</div>
	</div>
	<!-- 保存 -->
	<div class="preservation">
		<button class="btn zxue">保存</button>
	</div>
</body>
<script type="text/javascript" src="../../js/zepto.min.js"></script>
<script type="text/javascript" src="../../js/common.js"></script>
<script type="text/javascript" src="../../js/layer.js"></script>
<script type="text/javascript">

	Auth();

	var bianji1 = ['updateaddress','saveaddress'];
	var is_default,username,userphone,provincename,cityname,xxdz,id

	var ybdz = localStorage.getItem("ybdz");
	console.log(ybdz)

	var bianji = localStorage.getItem("bianji");
	if(bianji == 1){
		$('.head p').html('编辑收货人')
		xxdz = localStorage.getItem("xxdz");
	console.log(xxdz)

		xxdz = xxdz.split("|")
		username = xxdz[4];
		userphone = xxdz[5];
		provincename = xxdz[0] +xxdz[1]+ xxdz[2];
		cityname =  xxdz[3];
		is_default = xxdz[7];
		if(is_default == 1){
			$('.check').addClass("youtu")
		} else{
			$('.check').removeClass("youtu")
		}
		id = xxdz[6];
		$('.yznum').val(ybdz)
		if($('.yznum').val() == 'null'){
			$('.yznum').val('')
		}
		$('.username').val(username);
		$('.userphone').val(userphone);
		$('.shouhuodizhi_100').html(provincename);
		$('.address').val(cityname);
	} else{
		$('.head p').html('新增收货人')
	}


	$('.head-img').on('click',function(){
		window.history.go(-1);
	})

	/*选择所在区城*/
	/*点击次数*/
	var dianjishu=1;
	/*后台记录点击次数*/
	var dian="";
	/*地址ID*/
	var dianid="";
	/*地址*/
	var dizhi="";
	/*省号*/
    var shengid="";
    /*市号*/
    var shiid="";
    /*区号*/
    var quid="";
    $(".shouhuodizhi_100").click(function(){
    	$('.shengfenqu').show();
		$('.shengfendi').html('');
		shengid="";
	    shiid="";
	    quid="";
	    $.ajax({
			url:apicom + "mobile/after/memberaddress/getPositionList",
			// url:"http://192.168.30.231:10001/mobile/after/memberaddress/getPositionList",
		    type:"post",
	    	data:{
				uuid:user_info.uuid,
				phone:user_info.phone,
				type:dian,
				id:dianid
			},
			success:function(shuju){
				if(typeof(shuju) == 'string'){
					shuju = JSON.parse(shuju);
				}
				var canshu=shuju;
				var canshu=canshu.content;
				var xuanzedizhi="";

				dizhi="";
				$('.shouhuodizhi_100').html('选择所在省份、城市、县区');
				dianid="";
				for(var i in canshu){
					xuanzedizhi+='<li class="lidi"><span id="'+canshu[i].id+'" class="dizhil">'+canshu[i].name+'</span><span>&gt;</span></li>';
				}
				$('.shengfendi').html(xuanzedizhi);
				dian = 1;
			}
	    })
    })

    $(document).on('click','.lidi',function(e){
    	e.stopPropagation();
    	var dianid=$(this).find('span').attr("id");
        var dizhiw=$(this).find('.dizhil').html();
        var _this=$(this);
        $.ajax({
     	url:apicom + "mobile/after/memberaddress/getPositionList",
			// url:"http://192.168.30.231:10001/mobile/after/memberaddress/getPositionList",
        	type:"post",
        	data:{
        		uuid:user_info.uuid,
				phone:user_info.phone,
				type:dian,
				id:dianid
        	},
        	success:function(shuju){
        		if(typeof(shuju) == 'string'){
					shuju = JSON.parse(shuju);
				}
				var canshu=shuju;
				var canshu=canshu.content;
        		var xuanzedizhi="";
        		if(dianjishu == 1){
        			for(var i in canshu){
        				xuanzedizhi+='<li class="lidi"><span id="'+canshu[i].id+'" class="dizhil">'+canshu[i].name+'</span><span>&gt;</span></li>';
        			}
					$('.shengfendi').html(xuanzedizhi);
					dizhi+=dizhiw+'-'
                	shengid=dianid;
					dian=2;
					dianjishu++;
					shengid=_this.find('.dizhil').attr("id");
					console.log(shengid)
        		} else if(dianjishu==2) {
        			for(var i in canshu){
						xuanzedizhi+='<li class="lidi"><span id="'+canshu[i].id+'" class="dizhil">'+canshu[i].name+'</span><span>&gt;</span></li>';
					}
					$('.shengfendi').html(xuanzedizhi);
					dizhi+=dizhiw+'-'
					dianjishu++;
                	shiid=_this.find('.dizhil').attr("id");
					console.log(shiid)
        		} else if(dianjishu==3){
					e.stopPropagation();
					dizhi+=dizhiw;
					$('.shouhuodizhi_100').html(dizhi);
        			$('.shengfenqu').hide();
					dian='';
					dianid='';
					dianjishu=1;
                	quid=_this.find('.dizhil').attr("id");
					console.log(quid)
        		}
        	}
        })
    })
    // 用户名
	var usernameBol = false;
	var usernameStr = /^[\u4e00-\u9fa5]{2,25}$/;
	 $('.username').on('blur',function(){
	  if($('.username').val() == ''){
	    zhmBol = false;
	  	tips('收货人不能为空')
	  } else if(!usernameStr.test($('.username').val())){
	    tips('请输入正确收货人 2~25个字')
	    usernameBol = false;
	  } else{
	    usernameBol = true;
	  }
	 })
	 // 手机号判断
	var userphoneBol = false;
	var userphoneStr = /^[1][3,4,5,7,8][0-9]{9}$/;
	 $('.userphone').on('blur',function(){
	  if($('.userphone').val() == ''){
	    userphoneBol = false;
	  	tips('手机号不能为空')
	  } else if(!userphoneStr.test($('.userphone').val())){
	  	tips('请填写正确手机号')
	    userphoneBol = false;
	  } else{
	    userphoneBol = true;
	  }
 	})
	 // 收货地址
	var useraddressBol = false;
	 $('.address').on('blur',function(){
	  if($('.address').val() == ''){
	    useraddressBol = false;
	  	tips('收货地址不能为空')
	  } else{
	    useraddressBol = true;
	   }
 	})


	 var shengid = localStorage.getItem("proAreaId");
	 var quid = localStorage.getItem("districtAreaId");
	 var shiid = localStorage.getItem("cityAreaId");


    // 保存地址

    $('.btn').on('click',function(){
    	if($(this).hasClass('zxue')){
        	$(this).removeClass('zxue');
    	var bol = usernameBol&&userphoneBol&&useraddressBol
        console.log(usernameBol)
        console.log(userphoneBol)
        console.log(useraddressBol)
		var dzObj = {
					userName:$('.username').val(),
					mobile:$('.userphone').val(),
					proAreaId:shengid,
					districtAreaId:quid,
					cityAreaId:shiid,
					detailAddress:$('.address').val(),
					code:$('.yznum').val(),
//					id:id,
					phone:user_info.phone,
					uuid:user_info.uuid,
					status:is_default
				}
    	var lujing1 = '';
    	// var ybdz = 100000;

    	if(bianji == 1){
    		lujing1 = bianji1[0]
    		dzObj.id = id
    	} else{
    		lujing1 = bianji1[1]
    		dzObj.id = user_info.id
    	}
    	if($('.yznum').val().length != 0 && $('.yznum').val().length != 6){
    		tips('邮编格式不正确');
    		return false;
    	}
    	if($('.username').val() == '' || $('.userphone').val() == ''){
    		tips('填写信息不能为空哦~');
    		return false;
    	} else{
                    if(bol){
                        $.ajax({
                            url:apicom +"mobile/after/memberaddress/" +lujing1,
            //              url:"http://192.168.30.231:10001/mobile/after/memberaddress/"+lujing1,
                            type:"post",
                            data:dzObj,
                            success:function(shuju){
                                console.log(shuju)
                                if(shuju.code == 0){
                                    tips(shuju.msg)
                                }
                                if(typeof(shuju) == 'string'){
                                    shuju = JSON.parse(shuju);
                                }
                                var canshu = shuju;
                                window.location.href = rootdomain +'html/product-order/management.html'
                                // window.location.reload();

                            }
                        })
                    }else{
                        tips('收货地址不能为空');
                    }
    	}
    	setTimeout(function(){

    		 $('.btn').addClass('zxue');

    	},3000)
    	}

    })

  // 默认地址
  $('.check').on('click',function(){
 	$(this).toggleClass("youtu");
 	if($(this).hasClass("youtu")){
 		is_default = 1;
 	} else{
 		is_default = 0;
 	}
  })
</script>
</html>